﻿using System.Collections.Generic; 
using System.Linq; 
using DevExpress.Utils; 
using YDServices;
using YDModel;
using YDCommon;
using System;
using System.Windows.Forms;
using System.Drawing;
using static YDCommon.Utils;

namespace YDWin.Factory
{
    public partial class FactoryForm : DevExpress.XtraEditors.XtraForm
    {

        List<T_Factory> opList = null;
        T_Factory item = null;
        FactoryService svr = new FactoryService();
        //List<EnumberEntity> factoryTypeList = Utils.EnumToList<FactoryType>();
        SystemParameterService sps = new SystemParameterService();
        List<T_SystemParameter_List> factoryTypeList = new List<T_SystemParameter_List>();
        public FactoryForm()
        {
            InitializeComponent();

            Global.GirdViewSet(this.gvFactory); 

            //翻页事件
            pageControl1.OnPageChanged += new EventHandler(pagerControl1_OnPageChanged);
            //复选框加载的状态     实心Inactive   空心Unchecked   空心打勾InactiveChecked
            repositoryItemCheckEdit2.NullStyle = DevExpress.XtraEditors.Controls.StyleIndeterminate.Unchecked;
            //复选框选择事件
            repositoryItemCheckEdit2.CheckedChanged += new EventHandler(repositoryItemCheckEdit2_CheckedChanged);
  

        }
        /// <summary>
        /// 自动行号 自增长
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gvFactory_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
            if (e.Info.IsRowIndicator)
            {
                if (e.RowHandle >= 0)
                {
                    e.Info.DisplayText = (e.RowHandle + 1).ToString();
                }
                else if (e.RowHandle < 0 && e.RowHandle > -1000)
                {
                    e.Info.Appearance.BackColor = System.Drawing.Color.AntiqueWhite;
                    e.Info.DisplayText = "G" + e.RowHandle.ToString();
                }
            }
        }


        private void btnQuery_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LoadDataGrid();
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadDataGrid()
        {
            WaitDialogForm sdf = new WaitDialogForm("", "正在查询......");
            item = null;
            ConditionItem[] conds = GetConditions();
            if (conds != null)
            {
                opList = svr.getList(conds);
                sdf.Close();
                if (svr.HasError)
                {
                    Utils.ShowPrompt(MessageType.mtWarning, "载入失败\n" + svr.Errors);
                    svr.Errors.Clear();
                }
                else
                {
                    List<T_Factory> list = opList.Skip((pageControl1.PageIndex - 1) * pageControl1.PageSize).Take(pageControl1.PageSize).ToList();
                    pageControl1.DrawControl(opList.Count());
                    if (list.Count() == 0)
                    {
                        bsData.DataSource = null;
                    }
                    else
                    {
                        bsData.DataSource = list;
                        this.gvFactory.MoveFirst();//移动到第一条记录
                        //UIChecked.BestFit();
                    }
                }
            }


        }

        /// <summary>
        /// 查询条件
        /// </summary>
        /// <returns></returns>
        private ConditionItem[] GetConditions()
        {
            List<ConditionItem> conds = new List<ConditionItem>();

            conds.Add(new ConditionItem
            {
                PropertyName = "FactoryName",
                FindType = ConditionType.ctLike,
                Values = new object[] { txtFactory.Text }
            }); 

            return conds.ToArray();
        }

        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void pagerControl1_OnPageChanged(object sender, EventArgs e)
        {
            LoadDataGrid();
        }


        /// <summary>
        /// 选择变更事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void repositoryItemCheckEdit2_CheckedChanged(object sender, EventArgs e)
        {


        }

        /// <summary>
        /// 删除选中的记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //List<T_Factory> tuList = bsData.DataSource as List<T_Factory>;
            //tuList = tuList.Where(o => o.UIChecked == true).ToList();
            //if (tuList == null || tuList.Count < 1)
            //{
            //    Utils.ShowPrompt(MessageType.mtWarning, "请至少选择一条记录!");
            //    return;
            //}
            if (item == null) {
                Utils.ShowPrompt(MessageType.mtWarning, "请选择一条记录!");
                return;
            }
            DialogResult dr = Utils.ShowPrompt(MessageType.mtInformation, "即将删除该厂家信息,删除之后将不可恢复!", null, MessageBoxButtons.OKCancel);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            svr.DelItem(item);
            //svr.DelItem(tuList);
            if (svr.HasError)
            {
                Utils.ShowPrompt(MessageType.mtWarning, "删除失败\n" + svr.Errors);
                svr.Errors.Clear();
            }
            else
            {
                Utils.ShowPrompt(MessageType.mtInformation, "删除成功\n");
                LoadDataGrid();
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (item == null)
            {
                Utils.ShowPrompt(MessageType.mtWarning, "请选择一条记录!");
                return;
            }
            AddFactoryForm form = new AddFactoryForm();
            form.Text = "编辑厂家信息";
            form.state = "Edit";
            form.editTu = item;
            if (form.ShowDialog() == DialogResult.OK)
            {
                LoadDataGrid();
            }
        }

        /// <summary>
        /// 点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gvFactory_Click(object sender, EventArgs e)
        { 
            item = bsData.Current as T_Factory;
            //MessageBox.Show(string.Format("你单击了{0}!",item.FactoryName)); 
        }

        /// <summary>
        /// x新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            AddFactoryForm form = new AddFactoryForm();
            form.Text = "新增厂家信息";
            form.state = "Add";
            if (form.ShowDialog() == DialogResult.OK)
            {
                LoadDataGrid();
            }
        }

        private void FactoryForm_KeyPress(object sender, KeyPressEventArgs e)
        {
 

        }

        /// <summary>
        /// 显示转换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gvFactory_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName.Equals("FactoryType"))
            {
                if(e.Value!=null)
                { 
                    int val = (int)e.Value;
                    e.DisplayText = factoryTypeList.Where(o=>o.Value== val).Select(o=>o.Name).FirstOrDefault();
                }
               
            }
        }

        private void FactoryForm_Load(object sender, EventArgs e)
        {
            factoryTypeList = sps.getComboxList("OrderType");
        }
    }
}